<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head >
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>标签新增</title>
		<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
		<link href="https://cdnjs.cloudflare.com/ajax/libs/layer/2.3/skin/layer.css" rel="stylesheet">
		<link href="../static/css/index.css" rel="stylesheet" />
</head>
<body>

  <!--导航-->
<!--  <nav  class="ui inverted attached segment i-padded-tb-mini i-shadow-small" >
    <div class="ui container">
      <div class="ui inverted secondary stackable menu">
        <h2 class="ui teal header item">管理后台</h2>
        <a href="blogs.html" class="i-item item i-mobile-hide"><i class="mini home icon"></i>博客</a>
        <a href="types.html" class="i-item item i-mobile-hide"><i class="mini idea icon"></i>分类</a>
        <a href="tags.html" class="active i-item item i-mobile-hide"><i class="mini tags icon"></i>标签</a>
        <div class="right i-item i-mobile-hide menu">
          <div class="ui dropdown  item">
            <div class="text">
              <img class="ui avatar image" src="https://unsplash.it/100/100?image=1005">
              李仁密
            </div>
            <i class="dropdown icon"></i>
            <div class="menu">
              <a href="#" class="item">注销</a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <a href="#" class="ui menu toggle black icon button i-right-top i-mobile-show">
      <i class="sidebar icon"></i>
    </a>
  </nav>-->
  <div id="head"></div>
  <div class="ui attached pointing menu">
    <div class="ui container">
      <div class="right menu">
        <a href="tags-input.html" class="active item">新增</a>
        <a href="tags.html" class="teal  item">列表</a>
      </div>
    </div>
  </div>

  <!--中间内容-->
  <div  class="i-container-small i-padded-tb-big">
    <div class="ui container">
      <form action="#" method="post"   class="ui form" id="tagForm">
        <input type="hidden" name="id" >
        <div class=" field">
          <div class="ui left labeled input">
            <label class="ui teal basic label">名称</label>
            <input type="text" name="name" placeholder="标签名称" class="Required" id="name" v-model="tag.name">
          </div>
        </div>

       <div class="ui mini negative message" style="display: none;" id="checkForm">请检查必填项</div>
       
        <div class="ui right aligned container">
          <button type="button" class="ui button" onclick="window.history.go(-1)" >返回</button>
          <button class="ui teal submit button" v-on:click="submit()" type="button">提交</button>
        </div>

      </form>
    </div>
  </div>

  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <!--底部footer-->
  <div id="foot"></div>

  	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
		<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/layer/2.3/layer.js"></script>
		<!-- axios -->
		<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
		<script src="../static/js/function.js"></script>

  <script>

    $('.menu.toggle').click(function () {
      $('.i-item').toggleClass('i-mobile-hide');
    });

    $('.ui.dropdown').dropdown({
      on : 'hover'
    });

    $('.ui.form').form({
      fields : {
        title : {
          identifier: 'name',
          rules: [{
            type : 'empty',
            prompt: '请输入标签名称'
          }]
        }
      }
    });
    
    
    var pageType = '';
			var id = '';
			var tag = {
				'name': '',
				'id': '',
			}
			$(function() {
			$('#head').load('../common/head-admin.html',null,function(){
				$('.navbarlist a').removeClass('active');
				$('.navbarlist a:eq(2)').addClass('active');
			});
			$('#foot').load('../common/foot.html');				pageType = getQueryVariable("type");
				id = getQueryVariable("id");
				if(pageType == 'edit') {
					tagDetail();
				}
			})

			function tagDetail() {
				$.ajax({
					type: "get",
					url: urlPath + "/tag/api/edit_tag",
					data: {
						'id': id
					},
					success: function(res) {
						$('#name').val(res.data.name);
					}
				});
			}

			var vue = new Vue({
				el: "#tagForm",
				data: {
					tag
				},
				methods: {
					submit: function() {
						if(pageType == 'add') {
							checkRequired(tag);
							$.ajax({
								type: "post",
								contentType: 'application/json',
								url: urlPath + "/tag/api/save_tag",
								data: JSON.stringify(tag),
								success: function(res) {
									if(res.code === 200) {
										propSuccess(res, function() {
											window.location.href = "tags.html";
										})
									} else {
										
									}
								}
							});
						} else if(pageType == 'edit') {
							tag.name = $('#name').val().trim();
							tag.id = id;
							checkRequired(tag);
							$.ajax({
								type: "post",
								url: urlPath + "/tag/api/update_tag",
								contentType: 'application/json',
								async: true,
								data: JSON.stringify(tag),
								success: function(res) {
									if(res.code === 200) {
										propSuccess(res, function() {
											window.location.href = "tags.html";
										})
									}else{
										propFailReload(res, function() {
											window.location.reload();
										})
									}
								}
							});
						}
					}
				},
			})
    

  </script>
</body>
</html>